package com.ease.framework.gef4p.figures.impl {
   import com.ease.framework.flex.controls.Appender;
   import com.ease.framework.gef.IEditor;
   import com.ease.framework.gef4p.figures.INodeFigure;
   import com.ease.framework.gef4p.figures.edge.IEdgeFigure;
   import com.ease.framework.workbench.Workbench;

   import mx.collections.ArrayCollection;
   import mx.core.UIComponent;

   public class NodeFigure extends VertexFigure implements INodeFigure {
      private var _outEdges:ArrayCollection = new ArrayCollection();
      private var _inEdges:ArrayCollection = new ArrayCollection();

      protected var appenderOptions:Array = null;
      protected var appenders:Array = null;

      public function NodeFigure() {
         super();

//			prepareAppenderOptions();
      }

      override public function select():void {
//			showAppenders(true);

         super.select();
      }

      override public function deselect():void {
//			showAppenders(false);

         super.deselect();

         getLabelFigure().isSelected = false;
      }

      public function addOutEdge(edge:IEdgeFigure):void {
         outEdges.addItem(edge);
      }

      public function addInEdge(edge:IEdgeFigure):void {
         inEdges.addItem(edge);
      }

      public function removeInEdge(edge:IEdgeFigure):void {
         if (inEdges.contains(edge)) {
            inEdges.removeItemAt(inEdges.getItemIndex(edge));
         }
      }

      public function removeOutEdge(edge:IEdgeFigure):void {
         if (outEdges.contains(edge)) {
            outEdges.removeItemAt(outEdges.getItemIndex(edge));
         }
      }

      public function get outEdges():ArrayCollection {
         return _outEdges;
      }

      public function get inEdges():ArrayCollection {
         return _inEdges;
      }

      override public function onMoving():void {
         super.onMoving();
         var edge:IEdgeFigure;
         for each (edge in outEdges) {
            //edge.clearBends();
            IEdgeFigure(edge).refresh();
         }
         for each (edge in inEdges) {
            //edge.clearBends();
            IEdgeFigure(edge).refresh();
         }
      }

      protected function showAppenders(visible:Boolean):void {
         if (appenders == null) {
            appenders = createAppenders();
         }

         prepareAppender(appenders);

         for each (var u:UIComponent in appenders) {
            u.setVisible(visible);
         }

      }

      protected function prepareAppenderOptions():void {

      }

      /**
       * On startup, creates the various appenders that the user can interact with.
       **/
      protected function createAppenders():Array {
         var appenders:Array = new Array();

         if (appenderOptions == null)
            return appenders;

         for each (var option:Object in appenderOptions) {

            var appender:Appender = IEditor(Workbench.instance.currentEditor).getAppenderFactory().createAppender(option.type);

            appender.type = option.type;
            appender.visible = false;
            appender.container = this;
            appender.draw();

            addChild(appender);
            appenders.push(appender);

         }
         return appenders;
      }

      protected function prepareAppender(arr:Array):void {
         if (arr == null)
            return;

         var size:int = arr.length;
         var flag:int = 0;
         var appWidth:int = arr[i].width;
         var appHeight:int = arr[i].height;

         //right first 
         for (var i:int = 0; i < size; i++) {
            var appender:Appender = arr[i];

            if (appHeight * (i + 1) < height + 30) {
               appender.setStyle("top", (appHeight + 2) * i - 10);
               appender.setStyle("right", -35);
               appender.setStyle("bottom", null);
            }
            else {
               flag = i;
               break;
            }

         }

         if (flag >= size || i >= size)
            return;
         //bottom next
         for (i = flag; i < size; i++) {
            appender = arr[i];

            if (appWidth * (i - flag + 1) < width + 20) {
               appender.setStyle("top", null);
               appender.setStyle("right", appWidth * (i - flag));
               appender.setStyle("bottom", -35);
            }
            else {
               flag = i;
               break;
            }

         }

         if (flag >= size || i >= size)
            return;
         //top next
         for (i = flag; i < size; i++) {
            appender = arr[i];

            if (appWidth * (i - flag + 1) < width + 20) {
               appender.setStyle("top", -32);
               appender.setStyle("right", appWidth * (i - flag));
               appender.setStyle("bottom", null);
            }
            else {
               flag = i;
               break;
            }

         }

      }

   }
}